package org.example.operation;
import java.util.*;
import org.example.model.*;
import org.example.trans.*;
import org.example.inout.*;

/**
 * @author lenovo
 */

public class CourseOperation {
    //加载HashMap
    public static void loadCourse(){
        if(Save.getHashMapCourse() == null){
            Save.setHashMapCourse(TransScore.transScoreReadCourse(InOut.read
                    ("saveScore.txt")));
        }
        if(Save.getHashMapCourse() == null){
            Save.setHashMapCourse(new HashMap<>());
        }
    }
    //将courseName添加到HashMap中，并写入文件
    public static void addCourse(String courseName){
        loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Course> hashMap = Save.getHashMapCourse();
        Course course = new Course();
        course.setCourseName(courseName);
        hashMap.put(courseName,course);
        Save.setHashMapCourse(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
    //删除HashMap中的courseName，并写入文件
    public static void deleteCourse(String courseName){
        loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Course> hashMapCourse = Save.getHashMapCourse();
        HashMap<String,Score> hashMapScore = Save.getHashMapScore();
        hashMapCourse.remove(courseName);
        Save.setHashMapCourse(hashMapCourse);
        for(Map.Entry<String,Score> map : hashMapScore.entrySet()){
            map.getValue().getScore().remove(courseName);
            hashMapScore.put(map.getKey(),map.getValue());
        }
        Save.setHashMapScore(hashMapScore);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
    //检测HashMap中是否存在courseName
    public static boolean isCourse(String courseName){
        loadCourse();
        HashMap<String,Course> hashMap = Save.getHashMapCourse();
        return hashMap.containsKey(courseName);
    }
    //模糊查找HashMap中包含courseName的Map.Entry，返回String
    public static String searchCourse(String courseName){
        loadCourse();
        HashMap<String,Course> hashMap = Save.getHashMapCourse();
        StringBuilder stringBuilder = new StringBuilder();
        if(hashMap.containsKey(courseName)){
            stringBuilder.append(courseName).append("\n");
        }
        for(Map.Entry<String,Course> map : hashMap.entrySet()){
            if(!map.getKey().equals(courseName) && map.getKey().contains(courseName)){
                stringBuilder.append(map.getKey()).append("\n");
            }
        }
        return new String(stringBuilder);
    }
    //清空HashMap
    public static void clearCourse(){
        loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Course> hashMapCourse = Save.getHashMapCourse();
        HashMap<String,Score> hashMapScore = Save.getHashMapScore();
        hashMapCourse.clear();
        Save.setHashMapCourse(hashMapCourse);
        for(Map.Entry<String,Score> map : hashMapScore.entrySet()){
            map.getValue().getScore().clear();
            hashMapScore.put(map.getKey(),map.getValue());
        }
        Save.setHashMapScore(hashMapScore);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
}
